//
//  STTestLabelController.swift
//  iPhone
//
//  Created by Toj on 5/10/23.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class STTestLabelController: UIViewController {
    
    /* telLabel */
    private lazy var telLabel: UILabel = {
        let view = UILabel()
        view.text = "TEL.TEL.TEL.TEL.TEL.TEL.TEL.TEL.TEL.TEL.TEL.TEL.TEL.TEL.TEL.TEL"
        view.textColor = .white
        view.backgroundColor = .green
        view.setContentHuggingPriority(UILayoutPriority(100), for: .horizontal)
        return view
    }()
    
    /* numberLabel */
    private lazy var numberLabel: UILabel = {
        let view = UILabel()
        view.textColor = .red
        view.backgroundColor = .gray
        view.text = "1234567890"
        view.setContentHuggingPriority(UILayoutPriority(800), for: .horizontal)
        view.textAlignment = .right
        return view
    }()
    
    /* telLabel */
    private lazy var telLabel2: UILabel = {
        let view = UILabel()
        view.text = "TEL.TEL.TEL.TEL"
        view.textColor = .white
        view.backgroundColor = .green
        view.setContentHuggingPriority(.defaultLow, for: .horizontal)
        view.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        return view
    }()

    /* numberLabel */
    private lazy var numberLabel2: UILabel = {
        let view = UILabel()
        view.textColor = .red
        view.backgroundColor = .gray
        view.text = "1234567"
        view.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        view.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        view.textAlignment = .right
        return view
    }()
    
    /* topLabel */
    private lazy var topLabel: UILabel = {
        let view = UILabel()
        view.numberOfLines = 2
        view.text = "2222"
        view.textColor = .blue
        view.backgroundColor = .gray
        return view
    }()
    
    /* bottomLabel */
    private lazy var bottomLabel: UILabel = {
        let view = UILabel()
        view.text = "333"
        view.numberOfLines = 0
        view.textColor = .red
        view.backgroundColor = .gray
        return view
    }()
    
    /* topButton */
    private lazy var topButton: UIButton = {
        let view = UIButton()
        view.setTitleColor(.blue, for: .normal)
        view.setTitle("setTop", for: .normal)
        view.addTarget(self, action: #selector(didTapSetupTop), for: .touchUpInside)
        return view
    }()
   
    /* bottomButton */
    private lazy var bottomButton: UIButton = {
        let view = UIButton()
        view.setTitleColor(.red, for: .normal)
        view.setTitle("setBottom", for: .normal)
        view.addTarget(self, action: #selector(didTapSetupBottom), for: .touchUpInside)
        return view
    }()
    
    private lazy var marginView: UIView = {
        let view = UIView()
        view.backgroundColor = .yellow
        return view
    }()
    
    /* discountTipsLabel */
    private lazy var discountTipsLabel: UIInsetLabel = {
        let view = UIInsetLabel()
        view.numberOfLines = 3
        view.textColor = .white
        view.font = 12.to_systemFont
        view.textAlignment = .center
        view.preferredMaxLayoutWidth = CGFloat.sr_width - 80
        view.backgroundColor = UIColor.red.tt_alpha(0.5)
        view.cm_cornerRadius = 3
        return view
    }()
    
    /* discountTipsLabel */
    private lazy var discountTipsLabel2: UILabel = {
        let view = UILabel()
        view.numberOfLines = 3
        view.textColor = .white
        view.font = 12.to_systemFont
        view.textAlignment = .center
        view.preferredMaxLayoutWidth = CGFloat.sr_width - 80
        view.backgroundColor = UIColor.red.tt_alpha(0.5)
        view.cm_cornerRadius = 3
        return view
    }()
    
    /* title1Label */
    private lazy var title1Label: UILabel = {
        let view = UILabel()
        view.text = "*"
        view.numberOfLines = 0
        view.textColor = .white
        view.backgroundColor = .random
        view.font = 14.to_rbtMediumFont
        return view
    }()
    
    /* title2Label */
    private lazy var title2Label: UILabel = {
        let view = UILabel()
        view.text = "*\n*"
        view.numberOfLines = 0
        view.textColor = .white
        view.backgroundColor = .random
        view.font = 14.to_rbtMediumFont
        return view
    }()
    
    /* title3Label */
    private lazy var title3Label: UILabel = {
        let view = UILabel()
        view.text = "*\n*\n*"
        view.numberOfLines = 0
        view.textColor = .white
        view.backgroundColor = .random
        view.font = 14.to_rbtMediumFont
        return view
    }()
    
    /* title4Label */
    private lazy var title4Label: UILabel = {
        let view = UILabel()
        view.text = "*\n*\n*\n*"
        view.numberOfLines = 0
        view.textColor = .white
        view.backgroundColor = .random
        view.font = 14.to_rbtMediumFont
        return view
    }()
    
    private lazy var lyricList: [[String: String]] = {
        var list = [[String: String]]()
        
        
        return list
    }()
}

// MARK: - 生命周期
extension STTestLabelController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
}

private let discountTips = "23456789"
//发大水发顺丰发斯蒂芬阿萨法阿斯蒂芬23456789发大水发顺丰发斯蒂芬阿萨法阿斯蒂芬23456789发大水发顺丰发斯蒂芬阿萨法阿斯蒂芬\n23456789发大水发顺丰发斯蒂芬阿萨法阿斯蒂芬\n23456789发大水发顺丰发斯蒂芬阿萨法阿斯蒂芬"

// MARK: - 点击事件
@objc private extension STTestLabelController {
    
    func didTapSetupTop() {
        let length = discountTips.count
        let arcrandom = Int(arc4random_uniform(UInt32(length)))
        let text = discountTips.st_subsring(from: arcrandom)
        
        discountTipsLabel.text = text
        discountTipsLabel2.text = text
        
        print("-->toj: label text: \(text)")
    }
    
    func didTapSetupBottom() {
        let randomTop   : CGFloat = CGFloat(arc4random_uniform(16))
        let randomLeft  : CGFloat = CGFloat(arc4random_uniform(16))
        let randomBottom: CGFloat = CGFloat(arc4random_uniform(16))
        let randomRight : CGFloat = CGFloat(arc4random_uniform(16))
        let edgeInsets = UIEdgeInsets(top: randomTop, left: randomLeft, bottom: randomBottom, right: randomRight)
        
        let random = arc4random_uniform(1)
        let insets = random == 1 ? .zero : edgeInsets
        print("-->toj: label insets: \(insets)")
        discountTipsLabel.lbl_contentInsets = insets
    }
}
// MARK: - Private
private extension STTestLabelController {
    
}

// MARK: - 代理事件
extension STTestLabelController {
    
}

// MARK: - 初始化设置
private extension STTestLabelController {
    
    func setupMakeAddSubviews() {
        view.addSubview(telLabel)
        view.addSubview(numberLabel)
        
        view.addSubview(telLabel2)
        view.addSubview(numberLabel2)
        
        view.addSubview(topLabel)
        view.addSubview(bottomLabel)
        
        view.addSubview(topButton)
        view.addSubview(bottomButton)
        
        view.addSubview(marginView)
        view.addSubview(discountTipsLabel)
        view.addSubview(discountTipsLabel2)
        
        view.addSubview(title1Label)
        view.addSubview(title2Label)
        view.addSubview(title3Label)
        view.addSubview(title4Label)
    }
    
    func setupMakeLayoutSubviews() {
        telLabel.snp.makeConstraints { make in
            make.top.equalTo(100)
            make.height.equalTo(44)
            make.left.equalTo(view)
        }

        numberLabel.snp.makeConstraints { make in
            make.height.centerY.equalTo(telLabel)
            make.right.equalTo(view)
            make.left.equalTo(telLabel.snp.right).offset(2)
        }
        
        telLabel2.snp.makeConstraints { make in
            make.top.equalTo(numberLabel.snp.bottom).offset(20)
            make.height.equalTo(44)
            make.left.equalTo(view)
        }

        numberLabel2.snp.makeConstraints { make in
            make.height.centerY.equalTo(telLabel2)
            make.right.equalTo(view)
            make.left.equalTo(telLabel2.snp.right).offset(2)
        }
        
        topLabel.snp.makeConstraints { make in
            make.top.equalTo(numberLabel2.snp.bottom).offset(20)
            make.left.equalTo(view)
            make.width.equalTo(100)
        }
        
        bottomLabel.snp.makeConstraints { make in
            make.top.equalTo(topLabel.snp.bottom)
            make.left.width.equalTo(topLabel)
        }
        
        topButton.snp.makeConstraints { make in
            make.top.equalTo(bottomLabel.snp.bottom).offset(10)
            make.left.right.equalTo(view).inset(50)
            make.height.equalTo(44)
        }
        
        bottomButton.snp.makeConstraints { make in
            make.top.equalTo(topButton.snp.bottom).offset(10)
            make.left.height.right.equalTo(topButton)
        }
        
        discountTipsLabel.snp.makeConstraints { make in
            make.top.equalTo(bottomButton.snp.bottom).offset(10)
            make.left.equalTo(20)
        }
        
        marginView.snp.makeConstraints { make in
            make.top.equalTo(bottomButton.snp.bottom).offset(10)
            make.left.equalTo(0)
            make.width.equalTo(20)
            make.height.equalTo(30)
        }
        
        discountTipsLabel2.snp.makeConstraints { make in
            make.top.equalTo(topButton.snp.top).offset(-10)
            make.left.equalTo(discountTipsLabel)
        }
        
        title1Label.snp.makeConstraints { make in
            make.left.right.equalTo(view)
            make.top.equalTo(discountTipsLabel.snp.bottom).offset(10)
        }
        
        title2Label.snp.makeConstraints { make in
            make.left.right.equalTo(view)
            make.top.equalTo(title1Label.snp.bottom).offset(10)
        }
        
        title3Label.snp.makeConstraints { make in
            make.left.right.equalTo(view)
            make.top.equalTo(title2Label.snp.bottom).offset(10)
        }
        
        title4Label.snp.makeConstraints { make in
            make.left.right.equalTo(view)
            make.top.equalTo(title3Label.snp.bottom).offset(10)
        }
    }
    
    func setupMakeInitialize() {
        view.backgroundColor = .random
        
        _ = NSAttributedString(string: "")
  
        let lyric = "就像青春一样回不来"
        let lyricLabel = CLLrcLabel()
        lyricLabel.textColor = .white
        lyricLabel.progress = 0.5
        lyricLabel.text = lyric
        lyricLabel.font = 12.to_boldFont
        
        view.addSubview(lyricLabel)
        let y = view.bounds.height - 20
        let width = lyric.cm_stringOneLineWidth(font: 12.to_boldFont)
        lyricLabel.frame = CGRect(x: 0, y: y, width: width, height: 20)
    }
}
